﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using socialnetworkApp.Domain.Dto.Input;
using socialnetworkApp.Domain.Dto.Output;
using socialnetworkApp.Domain.IRepository;
using socialnetworkApp.Domain.Models;
using socialnetworkApp.Domain.RestResultModel;
using socialnetworkApp.Service.UsersService;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace socialnetworkApp.Controllers
{
    [Route("api/PostsService")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IUserService _userService;
        private readonly IMemoryCache _memoryCache;

       private readonly  IMapper _mapper;
        public TestController(IUserService userService,IMemoryCache memoryCache, IMapper mapper) {
            _userService = userService;
            _memoryCache = memoryCache;
            _mapper = mapper;   
        }
        [Route("/test")]
        [HttpGet]
        public async Task<object> Test(string id) {

            GetUserIfoInput getUserIfoInput1 = new GetUserIfoInput {Id =id };
       var  a=   await  _userService.GetFriendsList(getUserIfoInput1);
            return a;
        }
        [HttpGet("aa")]
        public object aa() {
            //string input = "从年12以上";
            //var match = Regex.Match(input, @"\d+");
            //int number=0;
            //if (match.Success)
            //{
            //   number= int.Parse(match.Value);
            //    Console.WriteLine(number);
            //}
            //return number;
            string salaryRange = "15,000-20,000";
            string[] parts = salaryRange.Split('-');

            string lowerValue = parts[0].Replace(",", ""); // 处理前面的值
            string upperValue = parts[1].Replace(",", ""); // 处理后面的值

            Console.WriteLine($"Lower Value: {lowerValue}");
            Console.WriteLine($"Upper Value: {upperValue}");
            return null;
        }


    
        /// <summary>
        /// AutoMapper示例
        /// </summary>
        /// <returns></returns>
        [HttpGet("automapperTest")]
        public object automapperTest() {
            
           List<Users> users =new List<Users>();
            Users users1 = new Users { Nickname = "掌声",Email ="123@qq.com",Signature="你号达瓦低洼地下撒达娃" ,Sex="男",HeadeImg ="dwadwadw"};
            Users users2 = new Users { Nickname = "掌声", Email = "123@qq.com", Signature = "你号达瓦低洼地下撒达娃", Sex = "男", HeadeImg = "dwadwadw" };
            users.Add(users1);            
            users.Add(users2);
           var  list = _mapper.Map<List<UserInfoOutput>>(users);
         var test=    _mapper.Map<List<Users>>(list);
            return  list;
        }
        //[HttpPost("cs")]
        //public async Task<ApiResult> cs([FromBody] Posts posts)
        //{
        //    _memoryCache.Get("");
           

        //}
        [HttpPost("uploadFile")]
        public async Task<IActionResult> UploadFile(List<IFormFile> file)
        {
            if (file == null || file.Count == 0)
            {
                return BadRequest("No file selected.");
            }
            foreach (var item in file)
            {
                var fileName = Path.GetFileName(item.FileName);
                DateTime dateTime= DateTime.Now;
                var filePath = Path.Combine("wwwroot", dateTime.ToString("yyyy-M-HH-mm-ffff")+".jpg");


                //文件存储路径
                string filesPath = string.Format("/Uploads/ArticleImage/{0}/{1}/", dateTime.ToString("yyyy"), dateTime.ToString("yyyyMM"));
                //获取当前web目录
                var webRootPath = "wwwroot/"; ;
                if (!Directory.Exists(webRootPath + filesPath))
                {
                    Directory.CreateDirectory(webRootPath + filesPath);
                }
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await item.CopyToAsync(stream);
                }
                using (var memoryStream = new MemoryStream())
                {
                    item.CopyTo(memoryStream);
                    var bytes = memoryStream.ToArray();
                    string base64str = Convert.ToBase64String(bytes);
                }
            }
          

            return Ok("File uploaded successfully.");
        }



    }
}
